﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FormPipeDeriDraw : System.Windows.Forms.Form
	{
		public FormPipeDeriDraw()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.PipeDeriInitParameterSetting();
			base.Icon = Resource.YArchitechIcon;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.CheckingTxtBoxDisText())
			{
				if (this.rbtnNewPipeDis.Checked)
				{
					this.pipeOff = new double?(Convert.ToDouble(this.txtBoxDeriDis.Text.Trim()));
				}
				else
				{
					this.pipeOff = null;
				}
				if (!double.TryParse(this.tblevel.Text.Trim(), out this.pipeLevel))
				{
					YJKMessageBox.Show("与原有管道高差输入数字格式不正确！");
					this.tblevel.Focus();
					this.tblevel.SelectAll();
					return;
				}
				this.PipeDeriUserSettingWrite();
				base.DialogResult = DialogResult.OK;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void rbtnNewPipeDis_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtnNewPipeDis.Checked && this.CheckingTxtBoxDisText())
			{
				this.txtBoxDeriDis.Enabled = true;
				this.pipeOff = new double?(Convert.ToDouble(this.txtBoxDeriDis.Text));
				this.pipeOffSet = DeriPipeOffSet.NewPipeOff;
			}
		}

		private void rbtnOriginalPipeDis_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbtnOriginalPipeDis.Checked && this.CheckingTxtBoxDisText())
			{
				this.pipeOffSet = DeriPipeOffSet.OriginalPipeOff;
				this.txtBoxDeriDis.Enabled = false;
			}
		}

		public double? PipeOff
		{
			get
			{
				return this.pipeOff;
			}
		}

		public DeriPipeOffSet PipeOffSet
		{
			get
			{
				return this.pipeOffSet;
			}
		}

		public double PipeLevel
		{
			get
			{
				return this.pipeLevel;
			}
		}

		private void PipeDeriInitParameterSetting()
		{
			try
			{
				this.txtBoxDeriDis.Text = ClassMultiPipeXMLInfo.GetSelectSingleNodeValue(FormPipeDeriDraw.path, "nodePipeDeriDraw/offDis");
				this.pipeOffSet = (DeriPipeOffSet)Convert.ToInt32(ClassMultiPipeXMLInfo.GetSelectSingleNodeValue(FormPipeDeriDraw.path, "nodePipeDeriDraw/offType"));
				this.tblevel.Text = ClassMultiPipeXMLInfo.GetSelectSingleNodeValue(FormPipeDeriDraw.path, "nodePipeDeriDraw/offLevel");
				if (this.tblevel.Text == "")
				{
					this.tblevel.Text = "0";
				}
				if (this.pipeOffSet == DeriPipeOffSet.OriginalPipeOff)
				{
					this.rbtnOriginalPipeDis.Checked = true;
					this.rbtnNewPipeDis.Checked = false;
					this.txtBoxDeriDis.Enabled = false;
				}
				if (this.pipeOffSet == DeriPipeOffSet.NewPipeOff)
				{
					this.rbtnNewPipeDis.Checked = true;
					this.rbtnOriginalPipeDis.Checked = false;
					this.txtBoxDeriDis.Enabled = true;
				}
			}
			catch (Exception)
			{
			}
		}

		private void PipeDeriUserSettingWrite()
		{
			try
			{
				ClassMultiPipeXMLInfo.UpdateSelectSingleNodeValue(FormPipeDeriDraw.path, "nodePipeDeriDraw/offDis", this.txtBoxDeriDis.Text);
				string text = FormPipeDeriDraw.path;
				string selectSingleNodeName = "nodePipeDeriDraw/offType";
				int num = (int)this.pipeOffSet;
				ClassMultiPipeXMLInfo.UpdateSelectSingleNodeValue(text, selectSingleNodeName, num.ToString());
				ClassMultiPipeXMLInfo.UpdateSelectSingleNodeValue(FormPipeDeriDraw.path, "nodePipeDeriDraw/offLevel", this.tblevel.Text);
			}
			catch (Exception)
			{
			}
		}

		private void FormPipeDeriDraw_Load(object sender, EventArgs e)
		{
			this.PipeDeriInitParameterSetting();
		}

		private bool CheckingTxtBoxDisText()
		{
			bool result = true;
			if (!this.regex.IsMatch(this.txtBoxDeriDis.Text.Trim()))
			{
				result = false;
				this.txtBoxDeriDis.Focus();
				YJKMessageBox.Show("输入数字格式不正确,应为正负整数或小数，且不能为空！");
				this.txtBoxDeriDis.SelectAll();
			}
			return result;
		}

		private void sign_Click(object sender, EventArgs e)
		{
			double num;
			if (double.TryParse(this.tblevel.Text, out num))
			{
				if (num > 0.0)
				{
					this.tblevel.Text = "-" + this.tblevel.Text;
					return;
				}
				if (num < 0.0)
				{
					this.tblevel.Text = this.tblevel.Text.Substring(1, this.tblevel.Text.Length - 1);
					return;
				}
			}
			else
			{
				YJKMessageBox.Show("输入数字格式不正确，不能进行正负变换！");
				this.tblevel.Focus();
				this.tblevel.SelectAll();
			}
		}

		private void FormPipeDeriDraw_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.PipeDeriUserSettingWrite();
		}

		public static string path = Path.Combine(Product.DataLocation, "HYDrawParallelPipeInfo.xml");

		private DeriPipeOffSet pipeOffSet;

		private double pipeLevel;

		private double? pipeOff;

		private Regex regex = new Regex("^[+-]?[0-9]+\\d*[.]?\\d*$");
	}
}
